﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SchedulerControl
{
    public partial class TimeBandControl : UserControl
    {
        public TimeBandControl()
        {
            InitializeComponent();
             
        }

       


        private string amTitle = "قبل از ظهر";
        private string pmTitle = "بعد از ظهر";
        private decimal panelDisplayHeight;
        void TimeBandCanvasSizeChanged(object sender, SizeChangedEventArgs e)
        {
            
            decimal w = (decimal)e.NewSize.Width;
            decimal h = (decimal)e.NewSize.Height;
            panelDisplayHeight = (decimal)e.NewSize.Height;
            this.timeBandCanvas.Children.Clear();
            bool isHourLine = true;
            
            int hour = 0;
            int minute;
            TextBlock timeLabel;
			
            for (decimal i = 0; i < h; i += (h / 24))
            {

                var hourLine = new Line
                                   {
                                       Stroke = new SolidColorBrush(Color.FromArgb(100, 162,162,162)),
                                       X1 = 0,
                                       Y1 = (double) i,
                                       X2 = (double) w,
                                       Y2 = (double) i,
                                       StrokeThickness = 1
                                   };
                timeLabel = new TextBlock();
				timeLabel.FontFamily=new FontFamily("/SchedulerControl;component/Fonts/Fonts.zip#B Mitra");
				timeLabel.FontSize = 14;
				timeLabel.Foreground = new SolidColorBrush(Color.FromArgb(255,200,200,200)); 
                Canvas.SetLeft(timeLabel, 5);
                Canvas.SetTop(timeLabel,(double)i);
                if (hour < 24)
                {
                    if (hour%12 == 0)
                    {
                        timeLabel.Text = "12:00";
                        if (hour < 12) timeLabel.Text += (" " + amTitle);
                        else timeLabel.Text += (" " + pmTitle);
                    }
                    else
                    {
                        timeLabel.Text = hour + ":00";
                    }
                }

                this.timeBandCanvas.Children.Add(timeLabel);
                this.timeBandCanvas.Children.Add(hourLine);
                ++hour;
            }

            
			decimal currentTimeHeight = DateTimeToHeight(DateTime.Now);

            var currentTimeLine = new Line
            {
                Stroke = new SolidColorBrush(Color.FromArgb(255, 251,162,6)),
                X1 = 0,
                Y1 = (double)currentTimeHeight,
                X2 = (double)w,
                Y2 = (double)currentTimeHeight,
                StrokeThickness = 2
            };
            this.timeBandCanvas.Children.Add(currentTimeLine);
            for (decimal i = (h / 48); i < h; i += (h / 24))
            {

                var hourLine = new Line
                                   {
                                       Stroke = new SolidColorBrush(Color.FromArgb(100, 162,162,162)),
                                       X1 = 0,
                                       Y1 = (double) i,
                                       X2 = (double) w,
                                       Y2 = (double) i,
                                       StrokeThickness = 0.5
                                   };
               
                this.timeBandCanvas.Children.Add(hourLine);
            }
        }

        private decimal DateTimeToHeight(DateTime dateTime)
        {
            int hour = dateTime.Hour;
            int minute = dateTime.Minute;
            decimal oneMinuteHeight = panelDisplayHeight / (24 * 60);
            decimal timeHeight = oneMinuteHeight * ((hour * 60) + minute);
            return timeHeight;
        }


        private DateTime HeightToDateTime(decimal height)
        {

            decimal oneMinuteHeight = panelDisplayHeight / (24 * 60);
            decimal minuteCount = height / oneMinuteHeight;
            int hour = (int)(minuteCount / 60);
            int minute = (int)(minuteCount % 60);
            DateTime dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 0);
            return dateTime;
        }

    }
}
